package com.yuntsg.nnsfcp.common.utils;


import com.yuntsg.nnsfcp.common.exceptions.Unlogin2Exception;
import com.yuntsg.nnsfcp.front.entity.Employee;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Locale;

/**
 * 用户工具类
 */
@Component
@Slf4j
public class UserUtil {

    public static final String KEY_LANG = "lang";
    public static final String KEY_USER = "user";
    private final static ThreadLocal<String> tlUser = new ThreadLocal<String>();
    private final static ThreadLocal<Locale> tlLocale = new ThreadLocal<Locale>() {
        protected Locale initialValue() {
            // 语言的默认值
            return Locale.CHINESE;
        }

        ;
    };
    @Resource
    HttpSession session;

    /**
     * 如果没有登录，返回null
     *
     * @return
     */
    public static String getUserIfLogin() {
        return tlUser.get();
    }

    /**
     * 如果没有登录会抛出异常
     *
     * @return
     */
    public static String getUser() {
        String user = tlUser.get();

        if (user == null) {
            throw new Unlogin2Exception();
        }

        return user;
    }

    public static void setUser(String userid) {
        tlUser.set(userid);

        // 把用户信息放到log4j
        MDC.put(KEY_USER, userid);
    }

    public static Locale getLocale() {
        return tlLocale.get();
    }

    public static void setLocale(String locale) {
        setLocale(new Locale(locale));
    }

    public static void setLocale(Locale locale) {
        tlLocale.set(locale);
    }

    public static void clearAllUserInfo() {
        tlUser.remove();
        tlLocale.remove();

        MDC.remove(KEY_USER);
    }


    //获取前台用户uid
    public Integer getFrontUid() {
        Object frontUserId = session.getAttribute("frontUserId");
        if (frontUserId == null) {
            throw new Unlogin2Exception();
        }
        return Integer.valueOf(frontUserId.toString());
    }

    public Integer getCheckFrontUid() {
        Object frontUserId = session.getAttribute("frontUserId");
        if (frontUserId == null) {
            return null;
        }
        return Integer.valueOf(frontUserId.toString());
    }

    // 这个只是在后台使用 不在区分单位 目前瑞金版本合并过来各种问先这样吧  2024年10月28日16:48:58 默认gid=1
    public Integer getGid() {
//        Object frontUserId = session.getAttribute("frontUserId");
//        if (frontUserId == null) {
//            throw new Unlogin2Exception();
//        }
        return 1;
    }

//    public Integer getUid() {
//        return userUtil.getUid();
//    }

    //获取前台用户gid 默认与uid 一样
    public Integer getFrontGid() {
        Object frontUserId = session.getAttribute("frontUserId");
        if (frontUserId == null) {
            throw new Unlogin2Exception();
        }
        return Integer.valueOf(frontUserId.toString());
    }

    public String getUserName() {
        Object frontUserId = session.getAttribute("frontUserName");
        if (frontUserId == null) {
            throw new Unlogin2Exception();
        }
        if (StringUtil.isNull(frontUserId.toString())) {
            frontUserId = "测试用户";
        }
        return (frontUserId.toString());
    }


    //获取前台用户名
    public String getFrontUname() {
        Object frontUserId = session.getAttribute("frontUserName");
        if (frontUserId == null) {
            throw new Unlogin2Exception();
        }
        if (StringUtil.isNull(frontUserId.toString())) {
            frontUserId = "测试用户";
        }
        return (frontUserId.toString());
    }

    //获取用户登录信息统一地址
    public Employee getBackUserInfo() {
        Employee employee = new Employee();
        Integer uid = 0;
        String username = "";
        try {
            uid = Integer.parseInt(session.getAttribute("uid").toString());
            username = session.getAttribute("username").toString();
        } catch (Exception e) {
//            e.printStackTrace();
            log.error("人员出错默认uid=1");
            username = "技术人员";
            uid = 1;
        }
        employee.setId(uid);
        employee.setUserName(username);
        return employee;
    }

    public Integer getUid() {
        Integer uid = 999;
        try {
            uid = getBackUserInfo().getId();
        } catch (Exception e) {

            uid = 1;
        }
        return uid;
    }

    public String getUidStr() {
        Integer uid = 999;
        try {
            uid = getBackUserInfo().getId();
        } catch (Exception e) {
            uid = 1;
        }
        return uid + "";
    }

    public Employee getUserEmployee() {

        Employee employee = new Employee();
        Integer uid = 0;
        String username = "";
        try {
            uid = Integer.parseInt(session.getAttribute("uid").toString());
            username = session.getAttribute("username").toString();
        } catch (Exception e) {
            e.printStackTrace();
            username = "技术人员";
            uid = 1;
        }
        employee.setId(uid);
        employee.setUserName(username);
        return employee;
    }

    public Integer gettype() {
        Integer type = 3;
        try {
            type = Integer.parseInt(session.getAttribute("type").toString());
        } catch (Exception e) {

        }
        return type;
    }
}
